package newsrecommendation.datamodel;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import newsrecommendation.datamodel.keywords.NewsKeywords;

/**
 *
 * @author Andrea
 */
public class NewsProduct implements Comparable<NewsProduct> {

    private String id;
    private Date publicationDate;
    private NewsKeywords keywords;

    public NewsProduct(String id, String publicationDate, String csv_Keywords) {
        this.id = id;
        setPublicationDate(publicationDate);
        keywords = new NewsKeywords(csv_Keywords);
    }

    public String getId() {
        return id;
    }

    public NewsKeywords getKeywords() {
        return keywords;
    }

    private void setPublicationDate(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
        try {
            publicationDate = sdf.parse(date);
        } catch (ParseException ex) {
            publicationDate = new Date();
        }
    }

    public Date getPublicationDate() {
        return publicationDate;
    }

    public int getPublishedDays() {
        Date d1 = this.publicationDate;
        Date d2 = new Date();
        int days = (int) ((d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
        return days > 0 ? days : 1;
    }

    //TODO - Borrar
    @Override
    public String toString() {
        return id + " : " + publicationDate + " : " + keywords.getActivatedKeywordsString();
    }

    @Override
    public int compareTo(NewsProduct t) {
        Date d1 = this.publicationDate;
        Date d2 = t.getPublicationDate();
        return d2.compareTo(d1);
    }
}
